<?php
class Pixel extends Model_Abstract{
	
	public function getAllPixels(){
		$sql = "select p.id as id, p.pos as pos, s.domain as domain from pixels as p left join sites as s on p.site_id = s.id";
		try{
			return $this->_connection->query($sql);
		}catch (Exception $e){
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	
	public function getPixelsById($id){
		$sql = "select p.id as id, p.pos as pos, p.pixel, s.id as siteId, s.domain as domain from pixels as p left join sites as s on p.site_id = s.id WHERE p.id=? LIMIT 1 ";
		try{
			$result = $this->_connection->query($sql, array($id));
			if(COUNT($result) > 0){
				return $result[0];
			}
			return array();
		}catch (Exception $e){
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	
	public function add($siteid, $pos, $pixel)
	{
		$sql = "insert into pixels set site_id=?, pos=?, pixel=?";
		try
		{
			return $this->_connection->execute($sql, array($siteid, $pos, $pixel));
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	public function updatePixelById($pixel_id, $site_id, $pos, $pixel){
		$sql = "update pixels set site_id=?, pos=?, pixel=? where id=?";
		try{
			return $this->_connection->execute($sql, array($site_id, $pos, $pixel, $pixel_id));
		}catch (Exception $e){
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	
	public function deletePixelById($pixel_id){
		$sql = "delete * from pixels where id=?";
		try{
			return $this->_connection->execute($sql, array($pixel_id));
		}catch (Exception $e){
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	
}